package com.example.nophase.activity.staff;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.Toolbar;

import com.example.nophase.R;
import com.example.nophase.adapter.TaskListActivityAdapter;
import com.example.nophase.api.RequstCommonApi;
import com.example.nophase.base.BaseActivity;
import com.example.nophase.common.BaseCallBack;
import com.example.nophase.common.BaseResp;
import com.example.nophase.common.LoginManager;
import com.example.nophase.entity.AdultItemDTO;
import com.example.nophase.entity.SysUserVO;
import com.example.nophase.utils.HttpUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Response;

public class StaffMainTaskListActivity extends BaseActivity {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.task_lv)
    ListView taskLv;

    public int page = 1;
    @BindView(R.id.iv_background)
    ImageView ivBackground;
    @BindView(R.id.task_lv_progressbar)
    ProgressBar taskLvProgressbar;
    private TaskListActivityAdapter adapter;
    private int dataSize;
    List<AdultItemDTO> resultList = new ArrayList<>();
    private List<AdultItemDTO> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_staff_main_task_list);
        ButterKnife.bind(this);
        initData();
        setToolbar();


        taskLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(StaffMainTaskListActivity.this, TaskFromActivity.class);
                intent.putExtra("forkey", resultList.get(position).getFormKey());
                startActivityForResult(intent, 1);
            }
        });

        taskLv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

                showWarningDialog(Integer.valueOf(list.get(position).getFormKey()));
                return true;
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
            initData(); //刷新操作
        super.onActivityResult(requestCode, resultCode, data);
    }

    //显示提醒框
    public void showWarningDialog(int applyForListId) {
        LayoutInflater factory = LayoutInflater.from(this);//提示框
        final View view = factory.inflate(R.layout.dialog_layout, null);//这里必须是final的
        AlertDialog dialog = new AlertDialog.Builder(this)
                .setTitle("是否删除")//提示框标题
                .setIcon(R.drawable.apply_for_from)
                .setMessage("是否删除退回的工单")
                .setPositiveButton("确定",//提示框的两个按钮
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                deleteItem(applyForListId);
                                dialog.dismiss();
                            }
                        })
                .setNegativeButton("取消", null).create();
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
    }

    private void initData() {
        resultList.clear();
        SysUserVO sysUserVO = LoginManager.getUserInfo(this);
        String uuid = sysUserVO.getUuid();
        HttpUtils.getInstance().setAuthToken(LoginManager.getToken(this));
        taskLvProgressbar.setVisibility(View.VISIBLE);
        Call<BaseResp<List<AdultItemDTO>>> call = HttpUtils.getInstance().getApiService(RequstCommonApi.class).loadAdultList("ANDROID", uuid);
        call.enqueue(new BaseCallBack<BaseResp<List<AdultItemDTO>>>() {
            @Override
            public void onSuccess(Response<BaseResp<List<AdultItemDTO>>> response) throws Exception {
                taskLvProgressbar.setVisibility(View.GONE);

                list = response.body().getData();
                dataSize = list.size();
                for (AdultItemDTO item : list) {
                    if (item.getTheme().indexOf("申请人:" + sysUserVO.getUsername()) > -1) {
                        ivBackground.setVisibility(View.GONE);
                        resultList.add(item);
                    }
                }
                adapter = new TaskListActivityAdapter(resultList, StaffMainTaskListActivity.this);
                taskLv.setAdapter(adapter);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(String message) {

            }
        });
    }


    private void setToolbar() {
        toolbar.setNavigationIcon(R.drawable.ic_left_black_24dp);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        getSupportActionBar().setHomeButtonEnabled(true);

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {

            case android.R.id.home:
                this.finish();
                return true;

        }
        return super.onOptionsItemSelected(item);
    }


    public void deleteItem(int id) {
        taskLvProgressbar.setVisibility(View.VISIBLE);
        Call<BaseResp<Object>> call = HttpUtils.getInstance().getApiService(RequstCommonApi.class).deleteApplyFor(id);
        call.enqueue(new BaseCallBack<BaseResp<Object>>() {
            @Override
            public void onSuccess(Response<BaseResp<Object>> response) throws Exception {
                taskLvProgressbar.setVisibility(View.GONE);
                ivBackground.setVisibility(View.GONE);
                System.out.println("=======" + response.body().toString());
                Toast.makeText(StaffMainTaskListActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
                initData();

            }

            @Override
            public void onFailure(String message) {

            }
        });
    }

}
